<?php

namespace app\admin\model;

use think\Model;
use think\Session;

class User extends Model
{
    /**
     * 状态常量
     */
    const STATE_ALLOW = 0;
    const STATE_BAN = 1;

    protected static $_stateList;
    protected $table = 'tp_user';

    /**
     * 用户登录
     * @param $userName 用户名称
     * @param $pwd 用户密码
     * @return array
     */
    public static function login($userName, $pwd)
    {
        if (empty($userName) || empty($pwd)) {
            return resError('参数错误');
        }

        $data = [
            'userName' => $userName,
            'userPwd' => md5($pwd),
        ];

        $validate = validate('User');
        if (!$validate->scene('login')->check($data)) {
            return resError($validate->getError());
        }

        $model = self::where($data)->find();
        if (empty($model)) {
            return resError('你输入的账号或密码不正确，请重新输入');
        }
        Session::set('adminUser', $model);
        return resData('登录成功', url('/admin/index'));
    }

    /**
     * 添加账号
     * @param array $data
     * @return array
     */
    public static function add(array $data)
    {
        if (empty($data)) {
            return resError('参数为空');
        }

        $validate = validate('User');
        if (!$validate->check($data)) {
            return resError($validate->getError());
        }

        $model = new self();
        $model->userName = trim($data['userName']);
        $model->userPwd = md5(trim($data['userPwd']));
        $model->created = time();
        $model->creater = 'admin';
        $model->state = isset($data['state']) && $data['state'] == 'on' ? 0 : 1;
        $model->grade = $data['grade'];

        if (!$model->save()) {
            return resError($model->getError());
        }
        return resData('添加成功', '', $model);
    }

    public static function updateUser(array $data)
    {
        if (empty($data) || empty($data['userId'])) {
            return resError('参数为空');
        }

        $validate = validate('User');
        if (!$validate->scene('edit')->check($data)) {
            return resError($validate->getError());
        }

        $model = User::get($data['userId']);
        $model->userPwd = md5($data['userPwd']);
        $model->state = isset($data['state']) && $data['state'] == 'on' ? 0 : 1;
        $model->grade = $data['grade'];
        if ($model->save() == 0) {
            return resError('更新失败');
        }
        return resData('更新成功', '', $model->toArray());

    }

    /**
     * 获取所有账号
     * @param array $condition
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function userList(array $condition)
    {
        $model = self::where($condition)->select();
        return $model;
    }

    public static function del($id)
    {
        return self::destroy($id);
    }

    /**
     * 获取状态列表
     * @return array
     */
    public static function getStateList()
    {
        if (self::$_stateList === null) {
            self::$_stateList = [
                self::STATE_ALLOW => '允许登录',
                self::STATE_BAN => '禁止登录',
            ];
        }

        return self::$_stateList;
    }

    /**
     * 获取状态信息
     * @return mixed|null
     */
    public function getStateMsgAttr()
    {
        $list = self::getStateList();
        return !empty($list) ? $list[$this->state] : null;
    }
}
